﻿Public Class frmJoinMem

    Dim sql As String = ""

    Private Sub frmJoinMem_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        dbConnSQL.Close()
        e.Cancel = True
        Me.Hide()
    End Sub

    Private Sub frmJoinMem_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Call gSubIconShow(Me)
        Call dbSqlConnection()
        Call txtReset()
        Dim policy As String = ""
        Try
            Using sr As New System.IO.StreamReader("policyMem.txt")
                policy += sr.ReadToEnd()
            End Using
        Catch ex As Exception
            MsgBox("The file could not be read:")
            MsgBox(ex.Message)
        End Try
        txtPolicyMem.Text = policy
        txtPolicyMem.Refresh()
    End Sub

    Private Sub Cancel_Click(sender As System.Object, e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    Private Sub tbnDupCheck_Click(sender As System.Object, e As System.EventArgs) Handles btnDupCheck.Click
        Dim isMem As Boolean
        If txtUserId.Text = "" Then
            MsgBox("아이디를 바르게 입력해 주세요")
            txtUserId.Focus()
            Exit Sub
        End If
        isMem = duplicateCheck(txtUserId.Text)
        If isMem = True Then
            MsgBox("이미 사용중인 아이디 입니다.")
            txtDupCheckResult.Text = ""
            txtUserId.Focus()
        Else
            MsgBox("사용 가능한 아이디 입니다.")
            txtDupCheckResult.Text = "enabled"
            txtPass.Focus()
        End If
    End Sub

    Private Sub OK_Click(sender As System.Object, e As System.EventArgs) Handles OK.Click
        If txtUserId.Text = "" Then
            MsgBox("아이디를 바르게 입력해 주세요")
            txtUserId.Focus()
            Exit Sub
        End If
        If txtDupCheckResult.Text = "" Then
            MsgBox("중복체크 버튼을 눌러 주세요")
            btnDupCheck.Focus()
            Exit Sub
        End If
        If txtPass.Text = "" Then
            MsgBox("비밀번호를 바르게 입력해 주세요")
            txtPass.Focus()
            Exit Sub
        End If
        If txtPass2.Text = "" Then
            MsgBox("비밀번호 확인을 바르게 입력해 주세요")
            txtPass2.Focus()
            Exit Sub
        End If
        If txtPass.Text <> txtPass2.Text Then
            MsgBox("비밀번호 확인이 일치 하지 않습니다. 바르게 입력해 주세요")
            txtPass.Focus()
            Exit Sub
        End If
        If txtEmail.Text = "" Then
            MsgBox("E-mail을 바르게 입력해 주세요")
            txtEmail.Focus()
            Exit Sub
        End If
        sql = "INSERT INTO InventoryManAuth(userId, userPass, userEmail, userNm, company) VALUES('" + txtUserId.Text + "','" + txtPass.Text + "','" + txtEmail.Text + "','" + txtName.Text + "','" + txtCompany.Text + "')"
        SQLSaveAction(sql, "회원가입이 완료 되었습니다")
        Me.Close()
    End Sub

#Region "프로시져"
    Private Sub txtReset()
        txtUserId.Clear()
        txtPass.Clear()
        txtPass2.Clear()
        txtEmail.Clear()
        txtName.Clear()
        txtCompany.Clear()
        txtDupCheckResult.Clear()
    End Sub

    Private Function duplicateCheck(ByVal uid As String) As Boolean

        Dim dsJoinMem As DataSet
        Dim daJoinMem As OleDb.OleDbDataAdapter
        Dim rs As String
        Dim query As String
        Dim result As Boolean

        query = "SELECT ISNULL(count(*),0) FROM InventoryManAuth WHERE userId='" + uid + "'"
        dsJoinMem = New DataSet
        daJoinMem = New OleDb.OleDbDataAdapter(query, dbConnSQL)
        daJoinMem.Fill(dsJoinMem, "isMem")

        'dbConn.Close()
        rs = dsJoinMem.Tables("isMem").Rows(0).Item(0)
        If CInt(rs) > 0 Then
            result = True
        Else
            result = False
        End If

        Return result
    End Function

    '## 쿼리를 저장하는 함수
    Public Sub SQLSaveAction(ByVal query As String, ByVal msg As String)

        Dim oleCmd As OleDb.OleDbCommand
        Dim oleTran As OleDb.OleDbTransaction
        oleCmd = New OleDb.OleDbCommand(query, dbConnSQL)

        oleTran = dbConnSQL.BeginTransaction
        oleCmd.Transaction = oleTran
        Try

            oleCmd.ExecuteNonQuery()
            oleTran.Commit()

            If msg <> "" Then
                MsgBox(msg)
            End If
        Catch err As OleDb.OleDbException
            oleTran.Rollback()
            dbConn.Close()
            MsgBox(query)

            MsgBox(err.Message, MsgBoxStyle.Critical, "오류")
        End Try
    End Sub
#End Region

    Private Sub chkAgreePolicyMem_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkAgreePolicyMem.CheckedChanged
        If chkAgreePolicyMem.Checked = True Then
            OK.Enabled = True
        Else
            OK.Enabled = False
        End If
    End Sub
End Class